﻿var page = {
    totalTime: 0,
    currentTime: 0,
    temperature: 0,
    CountDownTimer: 0,
    animationTimer: 0,
    progressBefore: 0,
    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {
        console.dir(event);
        var that = this;

        //接收上个界面的信息：时间，温度
        this.totalTime = event.time;
        this.currentTime = this.totalTime;
        this.temperature = event.temperature;
        this.setData({ temperature: { value: this.temperature + "C" } });
        this.setData({ time: { value: parseInt(this.currentTime / 60) + "分" + parseInt(this.currentTime % 60) + "秒" } });

        that.CountDownTimer = setInterval(function () {
            console.dir(that)
            console.log("currentTime : " + that.currentTime)
            // 计时结束
            if (that.currentTime <= 0) {
                if (that.CountDownTimer != 0) {
                    console.log("clear CountDownTimer....");
                    clearInterval(that.CountDownTimer);
                    console.log(that.effect)
                    pm.redirectTo("complete/complete");
                }
            }
            // 计时结束
            if (that.totalTime == 0) {
                progressBefore = 0;
            }
            else {
                progressBefore = that.currentTime * 1000 / that.totalTime;
                that.currentTime--;
                var progressAfter = that.currentTime * 1000 / that.totalTime;
                var progressPer = (progressBefore - progressAfter) / 10;
                that.setData({ CircleProgress: { value: progressBefore } });
                that.setData({ time: { value: parseInt(that.currentTime / 60) + "分" + parseInt(that.currentTime % 60) + "秒" } });
                that.progress_fun(progressBefore, progressPer);
            }
        }, 1 * 1000, "CountDownTimer")
    },

    progress_fun: function (progressBefore, progressPer) {
        var that = this;
        if (that.animationTimer != 0) {
            console.log("clear animationTimer00....");
            clearInterval(that.animationTimer);
            console.log("progressBefore : " + progressBefore.toString())
            if (progressBefore > 0) {
                that.animationTimer = setInterval(function () {
                    progressBefore -= progressPer;
                    that.setData({ CircleProgress: { value: progressBefore } });
                    console.log("progressBefore : " + progressBefore.toString())

                    if (progressBefore <= 0) {
                        progressBefore = 0;
                        console.log("clear animationTimer1....");
                        clearInterval(that.animationTimer);
                    }

                }, 100, "animationTimer")
            }
            else {
                console.log('time out finsh....')
            }
        }
        else {
            console.log("progressBefore : " + progressBefore.toString())
            that.animationTimer = setInterval(function () {
                progressBefore -= progressPer;
                that.setData({ CircleProgress: { value: progressBefore } });
                console.log("progressBefore : " + progressBefore.toString())

                if (progressBefore <= 0) {
                    progressBefore = 0;
                    console.log("clear animationTimer2....");
                    clearInterval(that.animationTimer);
                }

            }, 100, "animationTimer")
        }
    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {

    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
        if (this.CountDownTimer != 0) {
            clearInterval(this.CountDownTimer);
        }
        if (this.animationTimer != 0) {
            clearInterval(this.animationTimer);
        }
    },

    onBtn: function (event) {
        var that = this;
        console.log(that);
        if (that.CountDownTimer != 0) {
            console.log("clear CountDownTimersssssssssssss 4....");
            clearInterval(this.CountDownTimer);
        }
        if (that.animationTimer != 0) {
            console.log("clear animationTimessssssssssssssssssr 4....");
            clearInterval(this.animationTimer);
        }
        pm.navigateBack();
    },
};

Page(page);

page = 0;
